package net.lovesrc.allin.biz.gt;

import com.alibaba.fastjson.JSONObject;
import net.lovesrc.allin.Message;
import net.lovesrc.allin.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/gt")
public class GtController extends BaseController {

    @Autowired
    private GtService gtService;

    @GetMapping("/new-register")
    public Message newRegister(String registerPromotionCode) {
        gtService.newRegister(registerPromotionCode);
        return Message.success();
    }

    @GetMapping("/auth-id")
    public Message authId(String mobile) {
        gtService.authId(mobile);
        return Message.success();
    }

    @GetMapping("/sync-auth-id")
    public Message syncAuthId(String mobile) {
        gtService.syncStatus(mobile);
        return Message.success();
    }

    @PostMapping("/new-sys-jy")
    public Message newSysJy() {
        gtService.newSysJy(getCurrentUserId());
        return Message.success();
    }

    @PostMapping("/sync-my-account")
    public Message syncMyAccount() {
        List<JSONObject> rows = queryForRows(jdbcTemplate, "select mobile from v_gt_my_account");
        for (JSONObject row : rows) {
            String mobile = row.getString("mobile");
            gtService.getUserInfo(mobile);
        }
        return Message.success();
    }

    @PostMapping("/new-my-jy")
    public Message newJy(String buyerMobile, String sellerMobile, String autoSelectSeller) {
        boolean autoSelect = "on".equalsIgnoreCase(autoSelectSeller);
        if (autoSelect) {
            gtService.newToMyJyWithAutoSelectSeller(getCurrentUserId(), "ManualJy", buyerMobile);
        } else {
            gtService.newJy(getCurrentUserId(), "ManualJy", buyerMobile, sellerMobile);
        }
        return Message.success();
    }

    @GetMapping("/reset-pay-account")
    public Message resetPayAccount(String mobile) {
        gtService.tryResetPayAccount(mobile);
        return Message.success();
    }

    @GetMapping("/once")
    public Message once() {
        gtService.syncUserInfo("15300496210");
        return Message.success();
    }

}